Is wrapping a third party code the only solution to unit test its consumers? [closed]
Posted
by
Songo
on Programmers
See other posts from Programmers
or by Songo
Published on 2012-11-05T21:35:51Z
Indexed on
2012/11/06
17:21 UTC
Read the original article
Hit count: 228
I'm doing unit testing and in one of my classes I need to send a mail from one of the methods, so using constructor injection I inject an instance of Zend_Mail
class which is in Zend framework.
Now some people argue that if a library is stable enough and won't change often then there is no need to wrap it. So assuming that Zend_Mail
is stable and won't change and it fits my needs entirely, then I won't need a wrapper
for it.
Now take a look at my class Logger
that depends on Zend_Mail
:
class Logger{
private $mailer;
function __construct(Zend_Mail $mail){
$this->mail=$mail;
}
function toBeTestedFunction(){
//Some code
$this->mail->setTo('some value');
$this->mail->setSubject('some value');
$this->mail->setBody('some value');
$this->mail->send();
//Some
}
}
However, Unit testing demands that I test one component at a time, so I need to mock the Zend_Mail
class. In addition I'm violating the Dependency Inversion principle as my Logger
class now depends on concretion not abstraction.
Now is wrapping Zend_Mail
the only solution or is there a better approach to this problem?
The code is in PHP, but answers doesn't have to be. This is more of a design issue than a language specific feature
© Programmers or respective owner